<html>
<head><title>Calculator</title></head>
<body>

<h1>Calculator</h1>

<form method="get">
<input type="text" name="expr"><input type="submit" value="Calculate">
</form>

<?php
$equ = $_GET["expr"];
$valid10 = "#\-?\s*[0-9]*\s*\.?\s*[0-9]+\s*[\+\-\*\/]{1}\s*\-?\s*[0-9]*\s*\.?\s*[0-9]+\s*#";
$valid11 = "#\-?\s*[0-9]*\s*\.?\s*[0-9]+\s*#";
$valid = "#(\+\s*\+|\-\-|\/\s*\/|\*\s*\*)+#";
$valid2 = "#(0\/0\s*)#";
$valid3 = "#(0\/0\.\s*)#";
$valid4 = "#(0\/0\.\d+\s*)#";
$valid5 = "#^$#";
$valid6 = "#[A-Za-z\!\@\#\$\%\^\&\(\)\_\=\|\[\]\;\'\,\<\>\?\\#\~\`\{\}]#";
$valid7 = "#[\+\-\/\*]$#";
$valid8 = "#\.\d+\s*\.#";
$valid9 = "#\.\.#";
if(preg_match($valid, $equ) == 1 || preg_match($valid6, $equ) == 1 || preg_match($valid7,$equ)== 1 || preg_match($valid8,$equ) == 1 || preg_match($valid9,$equ))
{
	echo "Invalid input expression " . $equ;
}
elseif((preg_match($valid2,$equ) == 1 || preg_match($valid3,$equ) == 1) && preg_match($valid4,$equ) == 0)
{
	echo $equ . " = inf";
}
elseif(preg_match($valid5,$equ) == 1)
{
	echo "Enter an equation.";
}
elseif(preg_match($valid10,$equ) == 1 || preg_match($valid11,$equ) == 1)
{
	eval("\$ans = $equ ;");
	echo "Result \n";
	echo $equ . " = " . $ans;
}
else
{
	echo "Invalid input expression " . $equ;
}

?>
</body>
</html>
